// 读取图片 Base64 内容
export function TaImageToBase64(file: string, maxw: number = 500): Promise<string> {
	return new Promise((resolve, reject) => {

		// #ifdef H5
		let image = new Image();
		image.setAttribute('crossOrigin', 'Anonymous')
		image.onload = () => {
			let w = image.naturalWidth
			let h = image.naturalHeight
			let width = maxw > w ? w : maxw
			let height = width / (w / h)
			let canvas = document.createElement('canvas')
			canvas.width = width, canvas.height = height
			// @ts-ignore
			canvas.getContext('2d').drawImage(image, 0, 0, width, height)
			resolve(canvas.toDataURL('image/png'))
		}, image.src = file, image.onerror = () => {
			reject(new Error('UrlToBase64 Error'))
		}
		// #endif

		// #ifndef H5
		uni.compressImage({
			src: file, quality: 80, width: maxw + 'px',
			success: (ret: any) => uni.getFileSystemManager().readFile({
				encoding: 'base64', filePath: ret.tempFilePath,
				fail: (err: any) => reject(err), success: (res: any) => {
					resolve('data:image/jpeg;base64,' + res.data)
				}
			})
			
		})
		// #endif

	})
}